package com.wing.sms.controller;

import com.wing.common.utils.JsonResult;
import com.wing.sms.model.SmsCode;
import com.wing.sms.service.SmsCodeService;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.UserAuthVO;
import com.wing.web.annotation.AuthPass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * * 程序名 : SmsController
 * 模块 : 短信中心
 * 描述 : 短信接口
 * 修改历史
 * 序号 	       日期 		        修改人 		         修改原因
 */
@Validated
@RestController("SmsController")
@RequestMapping("sms/admin")
@AuthPass
@Slf4j
public class SmsController {

	@Autowired
	private SmsCodeService smsCodeService;

	@Autowired
	private UserClient userClient;
	
	/**
	 * 发送短信验证码
	 * 
	 * @param phone
	 * @return
	 */
	@GetMapping("/sendSmsCode")
	public JsonResult sendSmsCode(@RequestParam("phone") String phone) {

		try {

			JsonResult<UserAuthVO> jsonResult = userClient.findUserByPhone(Long.valueOf(phone));
			if(null == jsonResult || jsonResult.getCode() != 200){
				return JsonResult.fail("发送失败！用户不存在");
			}
			if(jsonResult.getData() == null){
				return JsonResult.fail("发送失败！用户不存在");
			}
			SmsCode smsCode = smsCodeService.generateCode(phone);
			return JsonResult.success("发送成功！",smsCode);
		}catch (Exception e){
			log.error("发送失败！", e);
			return JsonResult.fail("发送失败！"+e.getMessage());
		}

	}
	/**
	 * 发送指令校验码
	 *
	 * @param phone
	 * @return
	 */
	@GetMapping("/sendInstructCode")
	public JsonResult sendInstructCode(@RequestParam("phone") String phone,@RequestParam("code") String code) {

		try {
			JsonResult<UserAuthVO> jsonResult = userClient.findUserByPhone(Long.valueOf(phone));
			if(null == jsonResult || jsonResult.getCode() != 200){
				return JsonResult.fail("发送失败！用户不存在");
			}
			if(jsonResult.getData() == null){
				return JsonResult.fail("发送失败！用户不存在");
			}
			SmsCode smsCode = smsCodeService.instructCode(phone,code);
			return JsonResult.success("发送成功！",smsCode);
		}catch (Exception e){
			log.error("发送失败！", e);
			return JsonResult.fail("发送失败！"+e.getMessage());
		}

	}

	/**
	 * 发送短信通知
	 *
	 * @param phone
	 * @return
	 */
	@GetMapping("/noticeCode")
	public JsonResult noticeCode(@RequestParam("phone") String phone,@RequestParam("code") String code) {

		try {
			JsonResult<UserAuthVO> jsonResult = userClient.findUserByPhone(Long.valueOf(phone));
			if(null == jsonResult || jsonResult.getCode() != 200){
				return JsonResult.fail("发送失败！用户不存在");
			}
			if(jsonResult.getData() == null){
				return JsonResult.fail("发送失败！用户不存在");
			}
			SmsCode smsCode = smsCodeService.noticeCode(phone,code);
			return JsonResult.success("发送成功！",smsCode);
		}catch (Exception e){
			log.error("发送失败！", e);
			return JsonResult.fail("发送失败！"+e.getMessage());
		}

	}

	/**
	 * 发送库存不足短信通知
	 *
	 * @param phone
	 * @return
	 */
	@GetMapping("/alarmNoticeCode")
	public JsonResult alarmNoticeCode(@RequestParam("phone") String phone,@RequestParam("code") String code) {
		try {
			JsonResult<UserAuthVO> jsonResult = userClient.findUserByPhone(Long.valueOf(phone));
			if(null == jsonResult || jsonResult.getCode() != 200){
				return JsonResult.fail("发送失败！用户不存在");
			}
			if(jsonResult.getData() == null){
				return JsonResult.fail("发送失败！用户不存在");
			}
			SmsCode smsCode = smsCodeService.alarmNoticeCode(phone,code);
			return JsonResult.success("发送成功！",smsCode);
		}catch (Exception e){
			log.error("发送失败！", e);
			return JsonResult.fail("发送失败！"+e.getMessage());
		}

	}

}
